
// 引入模块
import COS from 'cos-js-sdk-v5'
import { post } from '@/api/request'

export default function cosupload(options, key) {
  var cos = new COS({
    SecretId: 'AKID2xhS8urzHOK7KcbiV9cu1hDYjjZEn9B3',
    SecretKey: 'pSuCJG2BD7rQp382rz1vjM8VjnkW7kfQ'
  })
  // 分片上传文件
  cos.putObject({
    Bucket: 'jiujiu-1258414065',
    Region: 'ap-guangzhou',
    Key: key,
    Body: options.file,
    onProgress: function(progressData) {
      progressData.percent *= 100
      console.log(JSON.stringify(progressData), 'progressData')
      options.onProgress(progressData)
    }
  }, function(err, data) {
    if (err) {
      // options.onError(error)
      console.log(err, 'err')
    } else {
      console.log(data, 'data')
      post('/upload/addfile', { 'file': { name: options.file.name, size: options.file.size }, path: key }).then(response => {
        console.log(response, 'response')
        options.onSuccess(response)
      }).catch(error => {
        console.log(error)
        options.onError(error)
      })
    }
  })
}